<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitealbum
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Controller.php 2011-08-026 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitealbum_Widget_FeaturedPhotosController extends Engine_Content_Widget_Abstract {

  public function indexAction() {

    // Get paginator
    $parentTable = Engine_Api::_()->getItemTable('album');
    $parentTableName = $parentTable->info('name');
    $table = Engine_Api::_()->getItemTable('album_photo');
    $tableName = $table->info('name');
    $select = $table->select()
                    ->from($tableName);
    if (!Engine_Api::_()->sitealbum()->isLessThan417AlbumModule()) {
      $select->joinLeft($parentTableName, $parentTableName . '.album_id=' . $tableName . '.album_id', null);
    } else {
      $select->joinLeft($parentTableName, $parentTableName . '.album_id=' . $tableName . '.collection_id', null);
    }
    $select->where($parentTableName . '.search = ?', true)
            ->where($tableName . '.featured = ?', 1)
            ->order('Rand()');

    $this->view->paginator = $paginator = Zend_Paginator::factory($select);
		$featuredPhoto = Engine_Api::_()->getApi('settings', 'core')->getSetting('sitealbum.featuredalbum', null);

    // Set item count per page and current page number
    $paginator->setItemCountPerPage($this->_getParam('itemCountPerPage', 4));
    $paginator->setCurrentPageNumber($this->_getParam('page', 1));


    // Do not render if nothing to show
    if (($paginator->getTotalItemCount() <= 0) || empty($featuredPhoto) ) {
      return $this->setNoRender();
    }
    $this->view->showLightBox = Engine_Api::_()->sitealbum()->showLightBoxPhoto();
    if ($this->view->showLightBox) {
      $this->view->params = $params = array('type' => 'featured', 'count' => $paginator->getTotalItemCount(), 'title' => $this->_getParam('title', 'Featured Photos'));
    }
  }

}
?>